.TH std::rbegin,std::crbegin 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::rbegin,std::crbegin \- std::rbegin,std::crbegin

.SH Synopsis
   Defined in header <array>
   Defined in header <deque>
   Defined in header <flat_map>
   Defined in header <flat_set>
   Defined in header <forward_list>
   Defined in header <iterator>
   Defined in header <list>
   Defined in header <map>
   Defined in header <regex>
   Defined in header <set>
   Defined in header <span>
   Defined in header <string>
   Defined in header <string_view>
   Defined in header <unordered_map>
   Defined in header <unordered_set>
   Defined in header <vector>
   template< class C >                          (since
   auto rbegin( C& c ) ->                       C++14)
   decltype(c.rbegin());                        (until
                                                C++17)
   template< class C >                          (since
   constexpr auto rbegin( C& c ) ->             C++17)
   decltype(c.rbegin());
   template< class C >                                  (since
   auto rbegin( const C& c ) ->                         C++14)
   decltype(c.rbegin());                                (until
                                                        C++17)
   template< class C >                                  (since
   constexpr auto rbegin( const C& c )                  C++17)
   -> decltype(c.rbegin());
   template< class T, std::size_t N >                           (since
   std::reverse_iterator<T*> rbegin( T                          C++14)
   (&array)[N] );                                               (until
                                                                C++17)
   template< class T, std::size_t N >                           (since
   constexpr std::reverse_iterator<T*>  \fB(1)\fP                     C++17)
   rbegin( T (&array)[N] );
   template< class T >                                                  (since
   std::reverse_iterator<const T*>          \fB(2)\fP                         C++14)
   rbegin( std::initializer_list<T> il                                  (until
   );                                                                   C++17)
   template< class T >
                                                \fB(3)\fP
   constexpr                                                            (since
   std::reverse_iterator<const T*>                                      C++17)
                                                        \fB(4)\fP
       rbegin( std::initializer_list<T>
   il );
   template< class C >                                                          (since
   auto crbegin( const C& c ) ->                                                C++14)
   decltype(std::rbegin(c));                                                    (until
                                                                \fB(5)\fP             C++17)
   template< class C >                                                          (since
   constexpr auto crbegin( const C& c )                                         C++17)
   -> decltype(std::rbegin(c));

   Returns an iterator to the reverse-beginning of the given range.

   1,2) Returns c.begin(), which is typically an iterator to the beginning of the
   sequence represented by c.
   1) If C is a standard Container, returns a C::reverse_iterator object.
   2) If C is a standard Container, returns a C::const_reverse_iterator object.
   3) Returns a std::reverse_iterator<T*> object to the reverse-beginning of array.
   4) Returns a std::reverse_iterator<const T*> object to the reverse-beginning of il.
   5) Returns std::rbegin(c), with c always treated as const-qualified.
   If C is a standard Container, returns a C::const_reverse_iterator object.

   range-rbegin-rend.svg

.SH Parameters

   c     - a container or view with a rbegin member function
   array - an array of arbitrary type
   il    - an std::initializer_list

.SH Return value

   1,2) c.rbegin()
   3) std::reverse_iterator<T*>(array + N)
   4) std::reverse_iterator<const T*>(il.end())
   5) c.rbegin()

.SH Exceptions

   May throw implementation-defined exceptions.

   Overloads

   Custom overloads of rbegin may be provided for classes and enumerations that do not
   expose a suitable rbegin() member function, yet can be iterated.

   Overloads of rbegin found by argument-dependent lookup can be used to
   customize the behavior of std::ranges::rbegin and                      \fI(since C++20)\fP
   std::ranges::crbegin.

.SH Notes

   The overload for std::initializer_list is necessary because it does not have a
   member function rbegin.

.SH Example


// Run this code

 #include <iostream>
 #include <iterator>
 #include <vector>

 int main()
 {
     std::vector<int> v = {3, 1, 4};
     auto vi = std::rbegin(v); // the type of “vi” is std::vector<int>::reverse_iterator
     std::cout << "*vi = " << *vi << '\\n';

     *std::rbegin(v) = 42; // OK: after assignment v[2] == 42
 //  *std::crbegin(v) = 13; // error: the location is read-only

     int a[] = {-5, 10, 15};
     auto ai = std::rbegin(a); // the type of “ai” is std::reverse_iterator<int*>
     std::cout << "*ai = " << *ai << '\\n';

     auto il = {3, 1, 4};
     // the type of “it” below is std::reverse_iterator<int const*>:
     for (auto it = std::rbegin(il); it != std::rend(il); ++it)
         std::cout << *it << ' ';
     std::cout << '\\n';
 }

.SH Output:

 *vi = 4
 *ai = 15
 4 1 3

.SH See also

   begin
   cbegin          returns an iterator to the beginning of a container or array
   \fI(C++11)\fP         \fI(function template)\fP
   \fI(C++14)\fP
   end
   cend            returns an iterator to the end of a container or array
   \fI(C++11)\fP         \fI(function template)\fP
   \fI(C++14)\fP
   rend            returns a reverse end iterator for a container or array
   crend           \fI(function template)\fP
   \fI(C++14)\fP
   ranges::rbegin  returns a reverse iterator to a range
   (C++20)         (customization point object)
   ranges::crbegin returns a reverse iterator to a read-only range
   (C++20)         (customization point object)
